package cn.bigcontainer.alt.meta.dao;

import cn.bigcontainer.alt.common.util.WoKit;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MDaoProvider {

	/**
	 * @param table
	 * @param pk
	 * @return
	 */
	public String getMaxColumn(String table, String pk) {
		String sql = String.format("select max(%s) from %s", pk, table);
		log.info(sql);
		return sql;
	}

	/**
	 *
	 * @param tcp
	 * @return
	 */
	public String getMax(String tcp) {
		String[] args = tcp.split(",");
		String table = args[0];
		String col = args[1];
		String prefix = args[2];
		String sql = String.format("select substring(max(%s), %d) from %s where %s like '%s", col,
				prefix.length() + 1, table, col, prefix) + "%'";
		log.info(sql);
		return sql;
	}

	/**
	 * 获取自定义sql
	 * 
	 * @param sql
	 * @return
	 */
	public String getSql(String sql) {
		log.info(sql);
		return sql;
	}

	/**
	 * 获取自定义sql
	 * 
	 * @param sql
	 * @return
	 */
	public String getSqlPaging (String sql, int start, int size) {
		sql += " limit " + start + "," + size;
		log.info(sql);
		return sql;
	}
	
	/**
	 * 获取sql的总行数
	 * 
	 * @param sql
	 * @return
	 */
	public String getCount(String sql) {
		String sql2 = "select count(*) from (" + sql + ") xyz";
		log.info(sql2);
		return sql2;
	}

	/**
	 * 获取sql的总行数
	 * @param table
	 * @param where
	 * @return
	 */
	public String getCountBy(String table, String where) {
		String sql = "select count(*) from " + table;
		if (!WoKit.isEmpty(where)) {
			sql += " where " + where;
		}
		log.info(sql);
		return sql;
	}
}
